import pprint

from bson import ObjectId
from config import col_activity, col_register, col_institution, col_alumni, col_activity_institution


def task_activity_contacts(act_id: str):
    """更新活动的参与人次"""
    act_id = ObjectId(act_id)
    contacts = col_register.find({"activity_id": act_id, "is_attended": True}, {"person_id": 1, "_id": 0})
    people_contacts = [str(p.get("person_id")) for p in contacts]

    """
        更新赋能组织人次
    """

    act_id = ObjectId(act_id)
    contacts = col_activity_institution.find({"activity_id": act_id, "is_attended": True}, {"institution_id": 1, "_id": 0})
    institution_contacts = [str(p.get("institution_id")) for p in contacts]

    col_activity.update_one({"_id": act_id}, {
        "$set": {
            "children": len(people_contacts),
            "people_num": len(set(people_contacts)),
            "institution_num": len(set(institution_contacts)),
            "institution_children": len(institution_contacts)
        }
    }
                            )


def task_activity_institution(act_id: str):
    """把部门加载到活动表 避免join cache """
    act_id = ObjectId(act_id)
    act_doc = col_activity.find_one({"_id": act_id})
    inst_id = act_doc.get("institution_id")
    if inst_id:
        inst_doc = col_institution.find_one({"_id": inst_id})

        col_activity.update_one({"_id": act_id}, {"$set": {
            "institution": {"_id": str(inst_doc.get("_id")),
                            "institution_name": inst_doc.get("institution_name")}}})
    # 更新协办
    co_inst_ids = act_doc.get("co_inst_ids")
    if co_inst_ids:
        inst_docs = col_institution.find({"_id": {"$in": co_inst_ids}})
        data = []
        for doc in inst_docs:
            data.append({"_id": str(doc.get("_id")), "institution_name": doc.get("institution_name")})

        col_activity.update_one({"_id": act_id}, {"$set": {"cost_labels": data}})

    return True


def task_alumni_join_act(person_id: str):
    person_id = ObjectId(person_id)
    num_of_activities = col_register.find({"person_id": person_id, "is_attended": True}).distinct('activity_id')
    col_alumni.update_one({"_id": person_id}, {"$set": {"num_of_activities": len(num_of_activities)}})


def task_alumni_belongs_permission(person_id: str):
    """把部门权限写入人身上"""
    person_id = ObjectId(person_id)
    activity_ids = col_register.find({"person_id": person_id}).distinct("activity_id")
    belongs = []
    for doc in col_activity.find({"_id": {"$in": activity_ids}, "is_deleted": {"$ne": True}},
                                 {"co_inst_ids": 1, "institution_id": 1}):
        if doc.get("institution_id"):
            belongs.append(doc.get("institution_id"))
        if doc.get("co_inst_ids"):
            belongs.extend(doc.get("co_inst_ids"))
    belongs = [str(b) for b in belongs]

    col_alumni.update_one({"_id": person_id}, {"$set": {'belongs_inst': belongs}})
    pprint.pprint(col_alumni.find_one({"_id": person_id}))


if __name__ == '__main__':
    for i in col_activity.find({"is_deleted": {"$ne": True}}):
        task_activity_contacts(str(i['_id']))
